package com.woniu.controller;

import com.woniu.Service.AccountService;
import com.woniu.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author Rain
 * @Description :
 * @date 2022/08/24 14:19
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    AccountService as;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Account> all = as.findAll();
        model.addAttribute("data", all);
        return "success";
    }

    @RequestMapping("/findByName")
    //将java转为json进行响应
    @ResponseBody
    public List<Account> findByName(String username, Model m) {
        List<Account> list = as.findByName(username);
        m.addAttribute("data", list);
        return list;
    }

    @RequestMapping("/findByIDs")
    public String findByIDs(Integer[] ids, Model m) {
        List<Account> list = as.findByIDs(ids);
        m.addAttribute("data", list);
        return "success";
    }

    @RequestMapping("/findByList")
    public String findByList(@RequestParam List<Integer> ids, Model m) {
        Integer[] ids2 = ids.toArray(new Integer[0]);
        List<Account> list = as.findByIDs(ids2);
        m.addAttribute("data", list);
        return "success";
    }

    @RequestMapping("/addOneForMap")
    public String addOneForMap(@RequestParam Map<String, Object> userinfo, Model m) {
        as.addOneForMap(userinfo);
        m.addAttribute("data", "新增用户" + userinfo);
        return "success";
    }

    @RequestMapping("/addOne")
    public String addOne(Account account, Model m) {
        as.addAccount(account);
        m.addAttribute("data", "新增用户" + account);
        return "success";
    }

    @ResponseBody
    @RequestMapping("/addOneJson")
    public Account addOneJson(@RequestBody Account account) {
        as.addAccount(account);

        return account;
    }


}
